# -*- coding: utf-8 -*-
"""
Created on Fri Aug 26 11:04:27 2022

@author: xiong
"""
import hashlib
import time


from datetime import date
from datetime import timedelta

def lastWday(adate, w):
    """Mon:w=0, Sun:w=6"""
    delta = (adate.weekday() + 6 - w) % 7 + 1
    return adate - timedelta(days=delta)

def convert(txt):
    day = date.today().day
    month = date.today().month
    year = date.today().year
    
    mdl = "周一，周二，周三，周四，周五，周六，周日".split("，")
    if "上" in txt:
        start = date(year=year, month=month, day=day) - timedelta(days=7)
        prev = lastWday(start, mdl.index(txt.replace("上","")))    
    elif txt in mdl:
        start = date(year=year, month=month, day=day)
        prev = lastWday(start, mdl.index(txt))    
    else:
        return "0000-00-00"
    return prev.strftime("%Y-%m-%d")


def sqlReplysTemplate(i,taskid,postid):

    sql = """INSERT INTO gather_facebook_reply 
            (vc_id,vc_target_user_id,vc_postid,vc_publisher_url,vc_publisher_nickname,vc_publisher_photo_url,vc_publisher_photo_url_local,text_reply_content,text_picture_url,text_picture_url_local,text_content,dt_pubdate,dt_gather_time,vc_task_type,vc_md5) 
             VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"""%(
            hashlib.md5(str(time.time()).encode(encoding='UTF-8')).hexdigest(),#vc_id
            "",#vc_target_user_id 
            i["post_id"],#vc_postid
            i["comment_user_url"],#vc_publisher_url
            i["comment_user_id"],#vc_publisher_nickname
            i["comment_user_img"],#vc_publisher_photo_url
            "",#vc_publisher_photo_url_local
            i["comment_text"],#text_reply_content
            i["comment_image_url"],#text_picture_url
            "",#text_picture_url_local
            "",#text_content
            convert(i["comment_time"]),#dt_pubdate
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i["dt_gather_time"])),#dt_gather_time
            "",#vc_task_type
            hashlib.md5((taskid+postid).encode(encoding='UTF-8')).hexdigest(),#vc_md5
                )
    return sql